#include "iooperationimpl.h"

CIoOperationImpl::CIoOperationImpl()
{
	m_pBufLen = 8 * 1024;
	m_bAsynrw = false;
	m_MultiThread = false;
	m_pBuf = NULL;
	memset(&m_fd, 0, sizeof(os_io_fd));
	m_pBuf = new unsigned char[m_pBufLen + 1]();
	m_lIoStartPos = 0;
	m_lIoExpectBytes = 0;


}
CIoOperationImpl::~CIoOperationImpl()
{

}
//////////////////////////////////////////////////////////////////////////
std_method_impl CIoOperationImpl::SetOpType(optype op) 
{
	m_Opt = op;
	return S_OK;
}
std_method_type_impl(optype) CIoOperationImpl::GetOpType()
{
	return m_Opt;
}
std_method_impl CIoOperationImpl::SetId(ULONG lId)
{
	m_id = lId;
	return S_OK;
}

std_method_type_impl(ULONG) CIoOperationImpl::GetId()
{
	return m_id;
}
std_method_impl CIoOperationImpl::SetResult(ERROR_CODE lErrorCode,
	ULONG lTransferedBytes)
{
	m_Errorcode			= lErrorCode;
	m_pTransferedBytes	= lTransferedBytes;

	return S_OK;
}
std_method_impl CIoOperationImpl::GetResult(ERROR_CODE *pErrorCode,
	ULONG *pTransferedBytes)
{
	*pErrorCode			= m_Errorcode;
	*pTransferedBytes	= m_pTransferedBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::SetIoParam(ULONG lIoStartPos, ULONG lIoExpectBytes) 
{
	if (lIoExpectBytes - lIoStartPos > m_pBufLen) {
		return E_FAIL;
	}
	m_lIoStartPos		= lIoStartPos;
	m_lIoExpectBytes	= lIoExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetIoParam(ULONG* pIoStartPos, ULONG* pIoExpectBytes) 
{
	*pIoStartPos	= m_lIoStartPos;
	*pIoExpectBytes = m_lIoExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::AttachIoBuf(BUFFER_PTR buf, ULONG lIoStartPos, ULONG lIoExpectBytes) 
{
	retrtpv(buf, E_FAIL)
	memcpy(m_pBuf + lIoStartPos, buf, lIoExpectBytes);

	SetIoParam(lIoStartPos, lIoExpectBytes);

	return S_OK;
}
std_method_impl CIoOperationImpl::SetIoLen(ULONG lIoSize)
{

	if (lIoSize == m_pBufLen && lIoSize) {
		return S_OK;
	}

	m_pBuf.dispose();
	m_pBufLen = lIoSize * 1024;
	m_pBuf = new unsigned char[m_pBufLen + 1]();

	return S_OK;
}
std_method_type_impl(ULONG) CIoOperationImpl::GetIoLen()
{
	return m_pBufLen;
}
std_method_type_impl(BUFFER_PTR) CIoOperationImpl::GetIo(ULONG Pos)
{
	return m_pBuf + Pos;
}
std_method_impl CIoOperationImpl::ClearBuf() 
{
	memset(m_pBuf, 0, m_pTransferedBytes);
	return S_OK;
}
std_method_impl CIoOperationImpl::SetResultInterface(IBase* lOperationPos)
{
	return S_OK;
}
std_method_impl CIoOperationImpl::GetResultInterface(IBase** pOperationPos)
{
	return S_OK;
}
std_method_impl CIoOperationImpl::Cancel()
{
	m_bAsynrw = false;
	memset(&m_fd, 0, sizeof(os_io_fd));
	return S_OK;
}

std_method_impl CIoOperationImpl::SetBind(os_io_fd fd)
{
	memcpy(&m_fd, &fd, sizeof(os_io_fd));
	return S_OK;
}
std_method_type_impl(os_io_fd*) CIoOperationImpl::GetBind()
{
	return &m_fd;
}

std_method_type_impl(const char*) CIoOperationImpl::GetAddress()
{
	return m_fd.address;
}
std_method_impl CIoOperationImpl::SetAddress(const char* addr)
{
	retrtpv(addr, E_FAIL)
	strcpy(m_fd.address, addr);
	return S_OK;
}
std_method_type_impl(unsigned long) CIoOperationImpl::GetConPort()
{
	return m_fd.port;
}
std_method_impl CIoOperationImpl::SetConPort(unsigned long uport)
{
	m_fd.port = uport;
	return S_OK;
}
std_method_impl CIoOperationImpl::Post() 
{
	return m_pEvent->OnOperationComplete(this);
}

std_method_impl CIoOperationImpl::SetAsynRW(bool bstart)
{
	m_bAsynrw = bstart;
	return S_OK;
}
std_method_type_impl(bool) CIoOperationImpl::IsAsynRWThread()
{
	return m_bAsynrw;
}
std_method_impl CIoOperationImpl::SetMultiThread(bool bstart)
{
	m_MultiThread = bstart;
	return S_OK;
}
std_method_type_impl(bool) CIoOperationImpl::GetMultiThread()
{
	return m_MultiThread;
}

void  CIoOperationImpl::BindEvent(IIoOperationEvents *pEvents)
{
	m_pEvent = pEvents;
}


